package ginhelper

import (
	"gitee.com/wcs2/common/constants/messages"
	"gitee.com/wcs2/common/models/uimessages"
	"github.com/gin-gonic/gin"
	"net/http"
)

// swagger:model
func Response(c *gin.Context, httpCode int, data interface{}, errCode string, msg string, param interface{}) {
	var isSuccess bool = true
	var message *uimessages.UIMessage = nil
	if errCode != messages.OK {
		isSuccess = false
		if msg == "" {
			msg = messages.GetMsg(errCode)
		}
		message = &uimessages.UIMessage{
			Level:     3,
			Code:      errCode,
			Msg:       msg,
			Parameter: param,
		}
	}

	c.JSON(httpCode, gin.H{
		"IsSuccess": isSuccess,
		"Data":      data,
		"Message":   message,
	})
}

func ResponseOK(c *gin.Context) {
	Response(c, http.StatusOK, nil, messages.OK, "", nil)
}

func ResponseOKWithData(c *gin.Context, data interface{}) {
	Response(c, http.StatusOK, data, messages.OK, "", nil)
}

func ResponseError(c *gin.Context, errCode string) {
	Response(c, http.StatusOK, nil, errCode, "", nil)
}

func ResponseErrorMessage(c *gin.Context, errCode string, errMsg string) {
	Response(c, http.StatusOK, nil, errCode, errMsg, nil)
}

func ResponseForXH(c *gin.Context, errCode string, errMsg string) {
	isSuccess := true
	if errCode != messages.OK {
		isSuccess = false
	}

	c.JSON(http.StatusOK, gin.H{
		"success":   isSuccess,
		"errorCode": errCode,
		"errorMsg":  errMsg,
	})
}
